﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace atmsystem_ss
{
    public partial class frmCreate_Account : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable("tblAccount");
        private DataTable dtCustomer = new DataTable("tblCustomer");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;

        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();// khai bao 1 command
            command.Connection = con; //ket noi
            command.CommandType = CommandType.Text;//khai bao kieu command
            command.CommandText = @"Select  * From tblAccount";
            da.SelectCommand = command;//gan commad cho da
            da.Fill(dt);//nap du lieu cho table
            
            command.CommandText = "select * from tblCustomer";
            da.SelectCommand = command;
            da.Fill(dtCustomer);
            cboID_Customer.DataSource = dtCustomer;
            cboID_Customer.ValueMember = "fldID_Customer";
            cboID_Customer.SelectedValue = "fldID_Customer";

        }

        public frmCreate_Account()
        {
            InitializeComponent();
        }

        private void frmCreate_Account_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            frmInformation_Account _frmInformation_Account = new frmInformation_Account();
            _frmInformation_Account.Show();
        }
        private bool IsNumber(String str) {
            foreach (Char c in str)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }
        private bool kiemtra()
        {
            kt = true;
            if (txtID_Account.Text == "")
            {
                txtID_Account.Focus();
                MessageBox.Show("Ban chua nhap Account ID !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            
            else if (IsNumber(Convert.ToString(txtID_Account.Text)) == false) {
                txtID_Account.Focus();
                MessageBox.Show("Account ID la mot chuoi so nguyen, moi ban nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (tontai() == false) {
                txtID_Account.Text = "";
                txtID_Account.Focus();
                MessageBox.Show("Account ID da ton tai, ban vui long nhap Account ID khac !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (txtPIN.Text == "")
            {
                txtPIN.Focus();
                MessageBox.Show("Ban chua nhap PIN !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (IsNumber(Convert.ToString(txtPIN.Text)) == false)
            {
                txtPIN.Focus();
                MessageBox.Show("PIN la mot chuoi 4 so nguyen, moi ban nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (Convert.ToString(txtPIN.Text).Length.Equals(4) == false)
            {
                txtPIN.Focus();
                MessageBox.Show("Chu y PIN gom 4 so, moi ban nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (cboID_Customer.Text == "")
            {
                cboID_Customer.Focus();
                MessageBox.Show("Ban chua chon Customer !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (txtCurrency.Text == "")
            {
                txtCurrency.Focus();
                MessageBox.Show("Ban chua nhap Currency !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (ckStatus.Checked == false)
            {
                ckStatus.Focus();
                MessageBox.Show("Ban chua check Status !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }
        private bool tontai()
        {
            dt.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblAccount where fldID_Account LIKE '%'+@fldID_Account+'%' ";
            command.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = txtID_Account.Text;
            da.SelectCommand = command;
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            return true;

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dt.NewRow();
                row["fldID_Account"] = txtID_Account.Text;
                row["fldPIN"] = txtPIN.Text;
                row["fldID_Customer"] = cboID_Customer.SelectedValue;
                row["fldCurrency"] = Convert.ToInt32(txtCurrency.Text);
                row["fldStatus"] = ckStatus.Checked;
                dt.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = con;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert tblAccount values (@fldID_Account, @fldPIN, @fldID_Customer, @fldCurrency, @fldStatus)";
                commandInsert.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50, "fldID_Account");
                commandInsert.Parameters.Add("@fldPIN", SqlDbType.NVarChar, 50, "fldPIN");
                commandInsert.Parameters.Add("@fldID_Customer", SqlDbType.NVarChar, 50, "fldID_Customer");
                commandInsert.Parameters.Add("@fldCurrency", SqlDbType.Int, 50, "fldCurrency");
                commandInsert.Parameters.Add("@fldStatus", SqlDbType.Bit, 50, "fldStatus");
                da.InsertCommand = commandInsert;
                da.Update(dt);
                MessageBox.Show("Ban da Insert thanh cong", "Insert Successed", MessageBoxButtons.OK);
                txtID_Account.Text = "";
                txtPIN.Text = "";
                txtID_Account.Text = "";
                txtCurrency.Text = "";
                ckStatus.Checked = false;
            }

        }

    }
}
